<template>
  <div class="details">
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="img in details.photo" :key="img.id">
        <img :src="img.url" />
      </van-swipe-item>
    </van-swipe>
    <!-- title板块 -->
    <div class="details-info">
      <div class="details-title">{{ details.title }}</div>
      <div class="details-infos">
        <div class="details-price">￥{{ details.price }}</div>
        <span class="details-sale">月销量:{{ details.saleNum }}</span>
      </div>
    </div>
    <van-divider>商品图文详情</van-divider>
    <img
      v-for="detailInfo in detailInfos"
      :key="detailInfo.id"
      :src="detailInfo.image.url"
    />
    <div class="end">没有更多了 -.-</div>
    <van-goods-action>
      <van-goods-action-icon icon="wap-home-o" text="首页" to="/" />
      <van-goods-action-icon icon="cart-o" text="购物车" @click="linkToCart" />
      <van-goods-action-button
        type="warning"
        color="#31a5fc"
        text="加入购物车"
        @click="showHandler"
      />
    </van-goods-action>
    <van-dialog
      v-model="show"
      title="提示"
      show-cancel-button
      class="showPrompt"
      @confirm="addHandler"
      @cancel="cancelHandler"
    >
      <p>商品名称:{{ details.title }}</p>
      <p>商品价格:{{ details.price }}</p>
      <p>商品数量:1</p>
    </van-dialog>
    <!-- 回到顶部 -->
    <ToTop />
  </div>
</template>

<script>
import Vue from "vue";
import {
  Swipe,
  SwipeItem,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Divider,
  Dialog
} from "vant";

Vue.use(Dialog);
Vue.use(Divider);
Vue.use(GoodsAction);
Vue.use(GoodsActionButton);
Vue.use(GoodsActionIcon);
Vue.use(Swipe);
Vue.use(SwipeItem);
import ToTop from "../components/ToTop";
export default {
  name: "Details",
  data() {
    return {
      details: {},
      detailInfos: [],
      show: false
    };
  },
  components: {
    ToTop
  },
  created() {
    this.$http.getDetail(this.$route.params.id).then(resp => {
      // console.log(resp);
      this.details = resp.detail;
      this.detailInfos = resp.detail.descContentList.filter(item => {
        return !item.text;
      });
    });
  },
  methods: {
    linkToCart() {
      this.$router.push({
        name: "cart"
      });
    },
    showHandler() {
      this.show = !this.show;
    },
    //mutations做改变
    addHandler() {
      const { id, title, price, image } = this.details;
      const goods = {
        id,
        title,
        price,
        image,
        amount: 1
      };
      this.$store.commit("AddToCart", goods);
    },
    cancelHandler() {
      console.log(" 土豪，多买点");
    }
  }
};
</script>

<style lang="scss" scoped>
.details {
  overflow: auto;
  height: 100%;

  .my-swipe .van-swipe-item {
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    background-color: #39a9ed;

    img {
      width: 100%;
    }
  }
  .details-info {
    background: #fff;
    padding-top: 5px;

    .details-title {
      padding: 10px;
      color: #333;
      font-size: 18px;
      line-height: 24px;
      font-weight: 400;
    }
    .details-infos {
      overflow: hidden;
    }
    .details-price {
      padding: 0 10px;
      font-size: 28px;
      color: #f63;
      font-weight: 700;
      float: left;
    }
    .details-sale {
      float: right;
      color: #b1a9a5;
      font-size: 12px;
      line-height: 36px;
      padding-right: 20px;
    }
  }
  .end {
    padding: 20px 0;
    text-align: center;
    letter-spacing: 2px;
    color: #999;
    font-size: 14px;
  }
  .showPrompt {
    color: #999;
    margin: 0;
    text-align: center;
    line-height: 36px;
  }
}
</style>
